const fs = require('fs');
const path = require('path');
const { rm } = require('shelljs');
const { handleRedux, handleMobx } = require('../scripts/templateTransform');

const generate = (hbs, events, cb = () => {}) => {
  const { basePath, storeType } = global.config;
  const comp = `MyComponent${Math.floor(Math.random() * 100)}`;

  const p = path.join(__dirname, './temp.hbs');
  fs.writeFileSync(p, hbs, { encoding: 'utf-8' });

  const json = {
    evts: events,
    hbs_path: p,
    target: basePath,
    componentName: comp,
  };

  if (storeType === 'mobx') {
    handleMobx(json);
    cb(1, path.join(basePath, comp));
  } else if (storeType === 'redux') {
    handleRedux(json);
    cb(1, comp);
  } else {
    cb(0, '', 'NOT MATCHED STORE_TYPE');
  }
  rm(p);
};

module.exports = generate;
